package com.service.impl;


import com.common.exception.db.UpdateException;
import com.dao.SelectFormDao;
import com.dao.TeacherDao;
import com.entity.*;
import com.service.SelectFormService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/10/20.
 */
@Service
public class SelectFormServiceImpl implements SelectFormService {
    @Autowired
    private SelectFormDao selectFormDao;
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public List<Subject> subjectList() {
        return selectFormDao.subjectList();
    }

    @Override
    public List<Location> locationList(String cityName) {
        String asdd = "";
        for (int a = 0; a < cityName.length(); a++) {
            asdd = String.valueOf(cityName.charAt(a));
            if (asdd.equals("市")) {
                cityName = cityName.replace("市", "");
            }
        }
        Integer cityId = selectFormDao.findCityIsBeing(cityName);
        if (cityId != null) {
            return selectFormDao.locationList(cityId);
        } else {
            return null;
        }

    }

    @Override
    public List<Location> locationWhereCityId(Integer cityId) {
        return selectFormDao.locationList(cityId);
    }

    @Override
    public List<City> cityList() {
        return selectFormDao.cityList();
    }

    @Override
    public List<School> schoolList(String cityName) {
        String asdd = "";
        for (int a = 0; a < cityName.length(); a++) {
            asdd = String.valueOf(cityName.charAt(a));
            if (asdd.equals("市")) {
                cityName = cityName.replace("市", "");
            }
        }
        Integer cityId = selectFormDao.findCityIsBeing(cityName);
        if (cityId != null) {
            return selectFormDao.schoolList(cityId);
        } else {
            return null;
        }
    }

    @Override
    public List<Major> majorList() {
        return selectFormDao.majorList();
    }

    @Override
    public List<BirthPlace> birthPlaceList() {
        return selectFormDao.birthPlaceList();
    }

    @Override
    public int TeacherIdentify(String userId, String filePath, String position) {
        if (position.equals("id_card_front"))
            return selectFormDao.TeacherIdentifyUpdata(userId, filePath, 0);
        else if (position.equals("id_card_back"))
            return selectFormDao.TeacherIdentifyUpdata(userId, filePath, 1);
        else if (position.equals("student_card"))
            return selectFormDao.TeacherIdentifyUpdata(userId, filePath, 2);
        else if (position.equals("other"))
            return selectFormDao.TeacherIdentifyUpdata(userId, filePath, 3);
        else return 0;

    }

    @Override
    public String findTeacherIdentify(String userId) {
        return selectFormDao.findTeacherIdentify(userId);
    }

    @Override
    public int selectRecord(Integer stdUserId, Integer stdId, Integer teaUserId, Integer teaId) {
        return selectFormDao.selectRecord(stdUserId, stdId, teaUserId, teaId);
    }

    @Override
    public int selectRecord2(Integer stdUserId, Integer stdId, Integer teaUserId, Integer teaId) {
        return selectFormDao.selectRecord2(stdUserId, stdId, teaUserId, teaId);
    }

    @Override

    public int insertRecord(String stdUserId, String TeaUserId, String UUID, String stdId, String teaId, String recordType) {

        return selectFormDao.insertRecord(stdUserId, TeaUserId, UUID, stdId, teaId, recordType);
    }

    @Override
    public int updateRecord(Integer recordId) {
        return selectFormDao.updateRecord(recordId);
    }

    @Override
    public List<AssociationInfo> findRecordStudent(String userId, int limit, String tp) {

        List<AssociationInfo> list = selectFormDao.findRecordStudent(userId, limit, tp);

        return list;

    }

    @Override
    public List<AssociationInfo> myAppointment(Integer userId, Integer type, Integer limit) {
        if (type == 1) {  //如果为1是我的预约  2是被申请
            return selectFormDao.findMyAppointment(userId, limit); //查预约
        } else {
            return selectFormDao.findApplied(userId, limit);   //查被申请
        }
    }

    @Override
    public List<AssociationInfo> findMyPublishAppointment(Integer userId, Integer id, Integer limit) {
        return selectFormDao.findMyPublishAppointment(userId, id, limit);
    }

    @Override
    public List<Teacher> findRecordTeacher(String userId, int limit) {
        List<Teacher> list = selectFormDao.findRecordTeacher(userId, limit);
        for (int j = 0; j < list.size(); j++) {

            StringBuffer strb = new StringBuffer();
            String[] sub = list.get(j).getSubjectId().split(",");
            for (int i = 0; i < sub.length; i++) {
                String res = teacherDao.findSubjectName(sub[i]);
                strb.append(res + ",");
            }
            strb.delete(strb.length() - 1, strb.length());
            list.get(j).setSubjectName(strb.toString());
        }
        return list;
    }

    @Override
    public int upPhoto(String userId, String url) {
        return selectFormDao.upPhoto(userId, url);
    }

    @Override
    public String findOpenId(String userId) {
        return selectFormDao.findOpenId(userId);
    }

    @Override
    public List<formIdInfo> selectOhterFormId(String userId) {
        return selectFormDao.selectOhterFormId(userId);
    }

    @Override
    public int insertFormId(String userId, String formId) {
        Date startTime = new Date();
        long asd = startTime.getTime();
        long sdd = asd + 24 * 60 * 60 * 1000 * 7;
        Date endTime = new Date(Long.valueOf(sdd) + 000);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return selectFormDao.insertFormId(userId, formId, f.format(startTime), f.format(endTime));
    }

    @Override
    public int updateFormId(Integer id) {
        int success = selectFormDao.updateFormId(id);
        return success;
    }

    @Override
    public int insertOpenId(String userId, String openid) {
        return selectFormDao.insertOpenId(userId, openid);
    }

    @Override
    public int saveMap(Integer recordId, String thisLocation, String aimLocation, String mapPath) {
        return selectFormDao.saveMap(recordId, thisLocation, aimLocation, mapPath);
    }

    @Override
    public String findRecordStatus(String RecordId) {
        return selectFormDao.findRecordStatus(RecordId);
    }


    @Override
    public BigDecimal findMoney(String userId) {
        return selectFormDao.findMoney(userId);
    }

    @Override
    public int addMoney(BigDecimal bigDecimal, String userId) {
        return selectFormDao.addMoney(bigDecimal, userId);
    }

    @Override
    public int savePath(Integer recordId, String thisLocation, String thisLat, String thisLog, String aimLocation, String aimLat, String aimLog) {
        int count = selectFormDao.findPathCount(recordId);
        if (count < 5) {
            count = selectFormDao.savePath(recordId, thisLocation, thisLat, thisLog, aimLocation, aimLat, aimLog);
            if (count > 0) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return -1;
        }
    }


}
